package dk.muldbjergskolen.gui;

import dk.muldbjergskolen.ctr.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;


import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpretKlasse extends JDialog
{
	private JTextField textField_KlasseNavn;
	private JComboBox<String> comboBox_Aargang;
	private KlasseCtr kC = new KlasseCtr();
	private JFrame frame;

	/**
	 * Create the dialog.
	 */
	public OpretKlasse(OpretKlasseListener listener)
	{
		opretKlasseListener = listener;

		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Opret Klasse");
		setBounds(100, 100, 314, 144);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						opret();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			{
				JLabel lblNewLabel = new JLabel("Klassenavn:");
				lblNewLabel.setBounds(10, 11, 70, 14);
				panel.add(lblNewLabel);
			}
			{
				textField_KlasseNavn = new JTextField();
				textField_KlasseNavn.setBounds(90, 8, 132, 20);
				panel.add(textField_KlasseNavn);
				textField_KlasseNavn.setColumns(10);
			}
			{
				JLabel lblForkortelse = new JLabel("Årgang:");
				lblForkortelse.setBounds(10, 42, 70, 14);
				panel.add(lblForkortelse);
			}
			{
				comboBox_Aargang = new JComboBox<String>();
				comboBox_Aargang.setModel(new DefaultComboBoxModel<String>(new String[] { "2012", "2013", "2014", "2015", "2016", "2017", "..." }));
				comboBox_Aargang.setBounds(90, 39, 132, 20);
				panel.add(comboBox_Aargang);
			}
		}
	}

	//
	// Event håndtering.
	//

	// "Lyttere"
	OpretKlasseListener opretKlasseListener;

	public interface OpretKlasseListener
	{
		void OnNewKlasse();
	}

	/***
	 * Kaldes når en ny klasse er lavet.
	 */
	private void OnNewKlasse()
	{
		if (opretKlasseListener != null)
			opretKlasseListener.OnNewKlasse();
	}
	
	// opretter en klasse
	public void opret()
	{
		try
		{
			int aargang = Integer.parseInt(comboBox_Aargang.getSelectedItem().toString());
			String navn = textField_KlasseNavn.getText();
			kC.opretKlasse(navn, aargang);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
			return;
		}

		OnNewKlasse();
		setVisible(false);
	}
	
}
